package com.atguigu.exam.controller;


import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Banner;
import com.atguigu.exam.service.BannerService;
import com.atguigu.exam.service.FileUploadService;
import com.atguigu.exam.service.impl.FileUploadServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController  // REST控制器，返回JSON数据
@RequestMapping("/api/banners")  // 轮播图API路径前缀
@CrossOrigin  // 允许跨域访问
@Tag(name = "轮播图管理", description = "轮播图相关操作，包括图片上传、轮播图增删改查、状态管理等功能")  // Swagger API分组
public class BannerController {
    @Autowired
    private BannerService bannerService;
    @Autowired
    private FileUploadService fileUploadService;

    @Operation(summary = "获取所有轮播图")
    @GetMapping("/list")
    public Result<List<Banner>> list(){
        LambdaQueryWrapper<Banner> bannerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bannerLambdaQueryWrapper.orderByAsc(Banner::getSortOrder);
        List<Banner> list = bannerService.list();
        return Result.success(list);
    }
    @Operation(summary = "获取激活状态的轮播图")
    @GetMapping("/active")
    public Result activeList(){
        List<Banner> list = bannerService.list(new LambdaQueryWrapper<Banner>().eq(Banner::getIsActive, true));
        return Result.success(list);
    }
    @Operation(summary = "根据id获取轮播图")
    @GetMapping("/{id}")
    public Result<Banner> getByid(@PathVariable Long id){
        Banner byId = bannerService.getById(id);
        return Result.success(byId);
    }
    @Operation(summary = "根据id删除轮播图信息")
    @DeleteMapping("/delete/{id}")
    public Result<Banner> delete(@PathVariable Long id){
        boolean b = bannerService.removeById(id);
        if (b){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }
    @Operation(summary = "添加或更新轮播图信息")
    @RequestMapping(value = {"/add","/update"},method = {RequestMethod.POST,RequestMethod.PUT})
    public Result saveorupdate(@RequestBody Banner banner){
        boolean b = bannerService.saveOrUpdate(banner);
        if (b){
            return Result.success("添加或更新成功");
        }else{
            return Result.error("添加或更新失败");
        }
    }
    @Operation(summary = "上传轮播图")
    @PostMapping("/upload-image")
    public Result uploadimage(MultipartFile file){
        String url = fileUploadService.uploadFile(file);
        return Result.success(url,"上传成功");
    }
    @Operation(summary = "更新轮播图的激活状态")
    @PutMapping("/toggle/{id}")
    public Result toggle(@PathVariable Long id,@RequestParam Boolean isActive) {
        LambdaUpdateWrapper<Banner> bannerLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        //设置要更新的字段
        bannerLambdaUpdateWrapper.set(Banner::getIsActive, isActive).set(Banner::getUpdateTime, new Date());
        //设置要查询的条件
        bannerLambdaUpdateWrapper.eq(Banner::getId, id);
        boolean update =bannerService.update(bannerLambdaUpdateWrapper);
        if (update){
            return Result.success("更新激活状态成功");
        }else {
            return Result.error("更新激活状态失败");
        }
    }
} 